/**
 * 时间口语化
 * 比如10秒前 => 刚刚
 */

export function timeText(date) {
  if (!date) {
    return console.error('Date is empty!')
  }
  const now = new Date().valueOf()
  // const past = typeof date === 'object' ? date.valueOf() : date
  const past = new Date(date).valueOf()
  const distance = now - past
  const inSeconeds = distance < 60 * 1000
  const inMinutes = distance < 60 * 60 * 1000
  const inHours = distance < 24 * 60 * 60 * 1000

  if (inSeconeds) {
    return 'Just'
  } else if (inMinutes) {
    const minutes = Math.floor(distance / (60 * 1000))
    return `${minutes} seconds ago`
  } else if (inHours) {
    const hours = Math.floor(distance / (60 * 60 * 1000))
    return `${hours} hours ago`
  } else {
    const days = Math.floor(distance / (24 * 60 * 60 * 1000))
    return `${days} days ago`
  }
}
